package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.maiji.cloud.entities.shopingmall.ShopingOrderEditLog;
import com.maiji.cloud.mapper.ShopingOrderEditLogMapper;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.shopingmall.ShopingOrderEditLogReqData;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.ResMetaData;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.ShopingOrderEditLogService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.utils.BaseService;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ShopingOrderEditLogServiceImpl extends ServiceImpl<ShopingOrderEditLogMapper, ShopingOrderEditLog> implements ShopingOrderEditLogService {

    @Override
    public boolean save(ShopingOrderEditLogReqData data) {
        ShopingOrderEditLog shopingOrderEditLog = new ShopingOrderEditLog();
        BeanUtils.copyProperties(data, shopingOrderEditLog);
        return insert(shopingOrderEditLog.setUuId(UUID_MD5.getUUID()).setEditDate(new Date()));
    }

    @Override
    public BaseMetaResDto<List<ShopingOrderEditLog>> findAll (ShopingOrderEditLogReqData data, ReqMetaData metaData) {
        EntityWrapper<ShopingOrderEditLog> entityWrapper = new EntityWrapper<>();
        if (StringUtil.isNotBlank(data.getOrderNo())) entityWrapper.like("order_no", data.getOrderNo());
        if (StringUtil.isNotBlank(data.getOperatorName())) entityWrapper.like("operator_name", data.getOperatorName());
        BaseService.dealWithEntityWrapper(entityWrapper, data.getEditDates(), "edit_date");
        Integer editType = data.getEditType();
        if (editType != null) {
            if (1 == editType) entityWrapper.isNotNull("delivery_fee_new");
            if (2 == editType) entityWrapper.isNotNull("amount_new");
            if (3 == editType) entityWrapper.isNotNull("consignee_new");
            if (4 == editType) entityWrapper.isNotNull("tel_new");
            if (5 == editType) entityWrapper.isNotNull("shoping_address_new");
        }
        entityWrapper.orderBy(data.getOrderBy(), data.getAsc());
        Page<ShopingOrderEditLog> page = selectPage(new Page<>(metaData.getPage(), metaData.getSize()), entityWrapper);
        ResMetaData resMetaData = new ResMetaData(page.getCurrent(), page.getSize(), page.getTotal());
        return new BaseMetaResDto<List<ShopingOrderEditLog>>(Status.SUCCESS).setData(page.getRecords()).setResMetaData(resMetaData);
    }

}
